//<p>给你一个二叉搜索树的根节点 <code>root</code> ，返回 <strong>树中任意两不同节点值之间的最小差值</strong> 。</p>
//
//<p>差值是一个正数，其数值等于两值之差的绝对值。</p>
//
//<p>&nbsp;</p>
//
//<div class="original__bRMd"> 
// <div> 
//  <p><strong>示例 1：</strong></p> 
//  <img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" /> 
//  <pre>
//<strong>输入：</strong>root = [4,2,6,1,3]
//<strong>输出：</strong>1
//</pre> 
// </div>
//</div>
//
//<p><strong>示例 2：</strong></p> 
//<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" /> 
//<pre>
//<strong>输入：</strong>root = [1,0,48,null,null,12,49]
//<strong>输出：</strong>1
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示：</strong></p>
//
//<ul> 
// <li>树中节点的数目范围是 <code>[2, 100]</code></li> 
// <li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> 
//</ul>
//
//<p>&nbsp;</p>
//
//<p><strong>注意：</strong>本题与 530：<a href="https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/">https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/</a> 相同</p>
//
//<div><li>👍 280</li><li>👎 0</li></div>

#include <bits/stdc++.h>
#include "help_struct.h"
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    int ans = 0x3f3f3f3f, pre = -0x3f3f3f3f;

public:
    int minDiffInBST(TreeNode* root) {
        preTrace(root);
        return ans;
    }

    // 利用前序遍历
    void preTrace(TreeNode* root) {
        if (root == nullptr) return;
        preTrace(root->left);
        ans = min(root->val - pre, ans);
        pre = root->val;
        preTrace(root->right);
    }
};

//leetcode submit region end(Prohibit modification and deletion)


int main() {
    Solution s;
    //vector<int> data{7, 1, 5, 3, 6, 4};
    //auto ans = s.twoSum(data,11);
    //cout<<endl;
}
